﻿using Simple.ISysRepository;
using Simple.ISysService;
using Simple.SysRepository;
using Simple.Tools;
using Simple.Tools.Extensions;
using Simple.Tools.SnowFlake;
using Simple.Tools.Verify;
using SimpleSaas.Common;
using SimpleSaas.Common.Execption;
using SimpleSaas.Entity;
using SimpleSaas.Entity.Dto;

namespace Simple.SysService
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public class SysTenantService : ISysTenantService
	{
		[Autowired]
		protected ISysTenantRepository sysTenantRepository { get; set; }

		[Autowired]
		protected ISysUserRepository sysUserRepository { get; set; }

		[Autowired]
		protected ISimpleMapper simpleMapper { get; set; }

		[Autowired]
		protected ISysDatabaseRepository sysDatabaseRepository { get; set; }

		[Autowired]
		protected ISysDepartmentRepository sysDepartmentRepository { get; set; }

		[Autowired]

		protected ISysAppRepository sysAppRepository { get; set; }
		[Autowired]
		protected ISysAppTenantRepository sysAppTenantRepository { get; set; }
		[Autowired]
		protected ISysMenuRepository sysMenuRepository { get; set; }

		



		public SysTenantService() { }

		/// <summary>
		/// 创建企业
		/// </summary>
		/// <param name="tenantDto"></param>
		[Transaction]
		public async Task AddAsync(SysTenantDto tenantDto)
		{
			var result = tenantDto.Verify();
			if (!result.Item2)
			{
				throw new BusinessException(result.Item1);
			}

			if (await sysDatabaseRepository.AnyAsync(database => !database.IsDeleted && database.Id == tenantDto.DatabaseId.ToInt64()))
			{
				throw new BusinessException("请选择正确的数据库配置!");
			}

			SysTenantEntity sysTenant = new SysTenantEntity();
			simpleMapper.Map(tenantDto, sysTenant);
			sysTenant.Id = Worker.GetId();

			//创建企业
			await sysTenantRepository.CreateAsync(sysTenant);

			//创建企业绑定app
			await sysAppRepository.BatchCreateBindAsync(tenantDto.AppIds, sysTenant.Id);

			//创建部门
			SysDepartmentDto sysDepartmentDto = new SysDepartmentDto();
			sysDepartmentDto.Name = tenantDto.TenantName;
			sysDepartmentDto.TenantId = sysTenant.Id.ToString();
			sysDepartmentDto.ParentId = "0";

			long depId=await sysDepartmentRepository.CreateAsync(sysDepartmentDto);

			//创建用户
			tenantDto.SysUser.TenantId = sysTenant.Id.ToString();
			tenantDto.SysUser.AdminFlag =false;
			tenantDto.SysUser.SysDeptList = new List<SysDepartmentDto>();
			tenantDto.SysUser.SysDeptList.Add(new SysDepartmentDto()
			{
				OwnerFlag = true,
				Id= depId.ToString()
			});

			List<long> appList=tenantDto.AppIds.Select(app=>app.ToInt64()).ToList();
			tenantDto.SysUser.SysMenuList = await sysMenuRepository.Where(menu =>!menu.IsDeleted && appList.Contains(menu.AppId)).Select(menu => 
			new SysMenuDto() 
			{ 
				Id=menu.Id.ToString(),
				AppId=menu.AppId.ToString(),
				
			}).ToListAsync();
			await sysUserRepository.AddAsync(tenantDto.SysUser);


			return;
		}

		/// <summary>
		/// 更新企业
		/// </summary>
		/// <param name="tenantDto"></param>
		public async Task UpdateAsync(SysTenantDto tenantDto)
		{
			var result = tenantDto.Verify();
			if (!result.Item2)
			{
				throw new BusinessException(result.Item1);
			}

			if (await sysDatabaseRepository.AnyAsync(database => !database.IsDeleted && database.Id == tenantDto.DatabaseId.ToInt64()))
			{
				throw new BusinessException("请选择正确的数据库配置!");
			}

			SysTenantEntity sysTenant =await this.sysTenantRepository.FirstOrDefaultAsync(t=>t.Id==tenantDto.Id.ToInt64());

			simpleMapper.Map(tenantDto, sysTenant);

			//创建企业
			await sysTenantRepository.UpdateAsync(sysTenant);

			//创建企业绑定app
			await sysAppRepository.BatchUpdateBindAsync(tenantDto.AppIds, sysTenant.Id);


			return;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="tenantDto"></param>
		public async Task SoftDeleteAsync(SysTenantDto tenantDto)
		{
			var result = tenantDto.Verify();
			if (!result.Item2)
			{
				throw new BusinessException(result.Item1);
			}

			if (await sysDatabaseRepository.AnyAsync(database => !database.IsDeleted && database.Id == tenantDto.DatabaseId.ToInt64()))
			{
				throw new BusinessException("请选择正确的数据库配置!");
			}

			SysTenantEntity sysTenant = await this.sysTenantRepository.FirstOrDefaultAsync(t => t.Id == tenantDto.Id.ToInt64());


			await sysTenantRepository.SoftDeleteAsync(sysTenant);

			//app
			await sysAppTenantRepository.SoftDeleteAsync(r=>r.TenantId== tenantDto.Id.ToInt64());

			//用户
			await sysUserRepository.SoftDeleteAsync(r=>r.TenantId== tenantDto.Id.ToInt64());


			return;
		}
	}
}
